package com.bdqn.controller;

import com.bdqn.dto.UserDto;
import com.bdqn.pojo.BackendUser;
import com.bdqn.pojo.DevUser;
import com.bdqn.service.UserService;
import com.bdqn.vo.ReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/Login")
    public String Login(){
        return "/login";
    }

    @PostMapping("/toLogin")
    @ResponseBody
    public ReturnVo toLogin(@RequestBody UserDto userDto, HttpSession session){
        if (userDto.getType().equals("1")){
            DevUser devUser = userService.login_dev(userDto);
            if (devUser != null){
                session.setAttribute("DEV_USER" , devUser);
                return ReturnVo.success("1",devUser);
            }else{
                return ReturnVo.error("登录失败账号或密码错误!");
            }
        }else{
            BackendUser backendUser = userService.login_backend(userDto);
            if (backendUser != null){
                session.setAttribute("BACKEND_USER" , backendUser);
                return ReturnVo.success("2" , backendUser);
            }else{
                return ReturnVo.error("登录失败账号或密码错误!");
            }
        }
    }


    @RequestMapping("/to_index")
    public String Index(){
        return "/index";
    }

    @GetMapping("/out")
    public String out(){

        return "redirect:/Login";
    }
}
